#! /usr/bin/env python
# -*- coding: UTF-8 -*-

import Exec
import os.path

def toLocalFile(file, data, add=0):
    '''
    Export the piped output of a command to a file
    @param file : the file can exists, or not
    @param data : must be the piped output of a command
    @param add : if set to 1, add something to file without deleting
    existing content (don't use it with tar)
    '''
    if add == 0:
        file = '>' + file
    Exec.runCmd('cat >' + file, out=0, input=data)

def toRemoteFile(file, data, host, user="default", port=22, key=None, add=0):
    '''
    Export the piped output of a command to a file on a remote
    host using secure shell protocol.
    To use this function, you must have created a ssh key-pair
    with `ssh-keygen -t dsa` and you must use an empty passphrase.
    Also, your public key, located in ~/.ssh/id_dsa.pub must be
    added to the ~/.ssh/authorized_keys file of the user wich
    you want to use to connect to the remote host.
    If your public key is not located in ~/.ssh/id_dsa.pub
    (eg. if it's a RSA encrypted key) you can specify another
    key location.
    @param file : path to the file you wan to write. It can exists, or not.
    @param data : must be the piped output of a command
    @param host : the host you want to reach
    @param user : the user you want to use (by default
    the connected user is used)
    @param port : The port you want to use (default : 22)
    @param key : the path of your public key (default : ~/.ssh/id_dsa.pub)
    if not in default path.
    @param add : if set to 1, add something to file without deleting
    existing content (don't use it with tar)
    '''
    if user == "default":
        user = Exec.runCmd('id -un')[1][0]
    Exec.runCmd('cat >' + file, out=0, remote_host=host, ssh_key=key, input=data)

def toSamba(file, data, host, user="default", password="default", add=0):
    '''
    Export the piped output of a command to a file on a remote
    host using smb protocol.
    @param file : path to the file you wan to write. It can exists, or not.
    @param data : must be the piped output of a command
    @param host : the host you want to reach
    @param user : the user you want to use (by default
    the connected user is used)
    @param password : the password you want to use (by default
    no password s used)
    @param add : if set to 1, add something to file without deleting
    existing content (don't use it with tar)
    '''
    if user == "default":
        user = Exec.runCmd('id -un')[1][0]
    if password != "default":
        user = user + ' -P ' + password
    Exec.runCmd('')

def toFtp(file, data, host, user="default", password="default", add=0):
    '''
    Export the piped output of a command to a file on a remote
    host using file transfert protocol.
    @param file : path to the file you wan to write. It can exists, or not.
    @param data : must be the piped output of a command
    @param host : the host you want to reach
    @param user : the user you want to use (by default
    the connected user is used)
    @param password : the password you want to use (by default
    no password s used)
    @param add : if set to 1, add something to file without deleting
    existing content (don't use it with tar)
    '''
    if user == "default":
        user = Exec.runCmd('id -un')[1][0]
    if password != "default":
        user = user + ':' + password
    Exec.runCmd('curl -u '+ user + ' ftp://' + host + '/' + file + '-T -', out=0)
